#include "TestTextBoxForm.h"



#define MULTILINE 0x1
#define HSCROLL 0x2
#define VSCROLL 0x4
#define NOHIDESEL 0x8

#define LEFTALIGN 0x10
#define MIDDLEALIGN 0x20
#define RIGHTALIGN 0x40

#define LOWERCASE 0xF000
#define UPPERCASE 0xF001
#define READONLY 0xF002
#define NUMBERONLY 0xF003
#define PASSWORD 0xF004
#define WANTRETURN 0xF005
#define WANTTAB 0xF006


static struct {
	WORD ID;
	const wchar_t* Name;
} CheckBoxInfo[] = {

	{ MULTILINE, L"MultiLine" },
	{ HSCROLL, L"HScroll" },
	{ VSCROLL, L"VScroll" },
	{ NOHIDESEL, L"NoHideSel" }, 
};


static struct {
	WORD ID;
	const wchar_t* Name;
} ButtonInfo[] = {

	{ LOWERCASE, L"Lowercase" },
	{ UPPERCASE, L"Uppercase" },
	{ READONLY, L"ReadOnly" },
	{ NUMBERONLY, L"NumberOnly" },
	{ PASSWORD, L"Password" },
	{ WANTRETURN, L"WantReturn" },
	{ WANTTAB, L"WantTab" }
};



TestTextBoxForm::TestTextBoxForm() : 
	m_btnLeftAlign(LEFTALIGN), 
	m_btnMiddleAlign(MIDDLEALIGN), 
	m_btnRightAlign(RIGHTALIGN) 
{

	m_textBox = nullptr;
	m_styleFlag = 0;

	this->CreatedEvent.Register(this, &TestTextBoxForm::OnCreated);
}



void TestTextBoxForm::OnCreated(CreatedEventArgs& ev) {

	this->SetSize(550, 400);

	m_btnLeftAlign.SetText(L"LeftAlign");
	m_btnLeftAlign.SetSize(100, 30);
	m_btnLeftAlign.SetPosition(0, 0);
	m_btnLeftAlign.SetParent(*this);
	m_btnLeftAlign.Create();
	m_btnLeftAlign.IsSelected(true);

	m_btnMiddleAlign.SetText(L"MiddleAlign");
	m_btnMiddleAlign.SetSize(100, 30);
	m_btnMiddleAlign.SetPosition(100, 0);
	m_btnMiddleAlign.SetParent(*this);
	m_btnMiddleAlign.Create();

	m_btnRightAlign.SetText(L"RightAlign");
	m_btnRightAlign.SetSize(100, 30);
	m_btnRightAlign.SetPosition(200, 0);
	m_btnRightAlign.SetParent(*this);
	m_btnRightAlign.Create();

	int x = 0;

	for (int index = 0; index < CheckBoxCount; ++index) {

		m_checkBox[index].SetID(CheckBoxInfo[index].ID);
		m_checkBox[index].SetText(CheckBoxInfo[index].Name);
		m_checkBox[index].SetSize(100, 30);
		m_checkBox[index].SetPosition(x, 30);
		m_checkBox[index].SetParent(*this);
		m_checkBox[index].Create();

		x += 100;
	}

	x = 0;
	int y = 60;

	for (int index = 0; index < ButtonCount; ++index) {

		m_button[index].SetID(ButtonInfo[index].ID);
		m_button[index].SetText(ButtonInfo[index].Name);
		m_button[index].SetSize(100, 30);
		m_button[index].SetPosition(x, y);
		m_button[index].SetParent(*this);
		m_button[index].Create();

		x += 100;

		if (x >= 500) {
			x = 0;
			y += 30;
		}
	}

	CreateTextBox();
}



LRESULT TestTextBoxForm::WndProc(const WndMsg& msg) {

	switch (msg.Message) {

	case WM_COMMAND:
		if (msg.WParamHigh == BN_CLICKED) {

			for (int index = 0; index < CheckBoxCount; ++index) {

				if (msg.WParamLow == m_checkBox[index].GetID()) {

					if (m_checkBox[index].GetCheckState() == CheckState::Checked) {
						m_styleFlag |= CheckBoxInfo[index].ID;
					}
					else {
						m_styleFlag &= ~CheckBoxInfo[index].ID;
					}

					CreateTextBox();

					return 0;
				}
			}

			switch (msg.WParamLow) {

			case LOWERCASE:
				m_textBox->IsLowercase( ! m_textBox->IsLowercase());
				return 0;

			case UPPERCASE:
				m_textBox->IsUppercase( ! m_textBox->IsUppercase());
				return 0;

			case READONLY:
				m_textBox->IsReadOnly( ! m_textBox->IsReadOnly());
				return 0;

			case PASSWORD:
				m_textBox->IsPassword( ! m_textBox->IsPassword());
				return 0;

			case NUMBERONLY:
				m_textBox->IsNumberOnly( ! m_textBox->IsNumberOnly());
				return 0;

			case WANTRETURN:
				m_textBox->AcceptReturn( ! m_textBox->AcceptReturn());
				return 0;

			case WANTTAB:
				m_textBox->AcceptTab( ! m_textBox->AcceptTab());
				return 0;

			case LEFTALIGN:
				if (m_btnLeftAlign.IsSelected() == true) {
					m_styleFlag |= LEFTALIGN;
					m_styleFlag &= ~MIDDLEALIGN;
					m_styleFlag &= ~RIGHTALIGN;
					CreateTextBox();
				}
				return 0;

			case MIDDLEALIGN:
				if (m_btnMiddleAlign.IsSelected() == true) {
					m_styleFlag |= MIDDLEALIGN;
					m_styleFlag &= ~LEFTALIGN;
					m_styleFlag &= ~RIGHTALIGN;
					CreateTextBox();
				}
				return 0;

			case RIGHTALIGN:
				if (m_btnRightAlign.IsSelected() == true) {
					m_styleFlag |= RIGHTALIGN;
					m_styleFlag &= ~LEFTALIGN;
					m_styleFlag &= ~MIDDLEALIGN;
					CreateTextBox();
				}
				return 0;
			}
		}
		break;
	}

	return Form::WndProc(msg);
}



void TestTextBoxForm::CreateTextBox() {

	if (m_textBox != nullptr) {
		delete m_textBox;
	}

	m_textBox = new TextBox();

	if (m_styleFlag & LEFTALIGN) {
		m_textBox->SetTextAlign(TextAlign::Left);
	}
	else if (m_styleFlag & MIDDLEALIGN) {
		m_textBox->SetTextAlign(TextAlign::Center);
	}
	else if (m_styleFlag & RIGHTALIGN) {
		m_textBox->SetTextAlign(TextAlign::Right);
	}

	m_textBox->IsMultiLine(m_styleFlag & MULTILINE);
	m_textBox->IsAutoHorizontalScroll(m_styleFlag & HSCROLL);
	m_textBox->IsAutoVerticalScroll(m_styleFlag & VSCROLL);
	m_textBox->NoHideSelection(m_styleFlag & NOHIDESEL);

	m_textBox->SetSize(200, 200);
	m_textBox->SetPosition(50, 150);
	m_textBox->SetParent(*this);
	m_textBox->Create();
}

